/**
 * 
 */
package com.ph.product.api.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.ph.base.BaseController;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.RoleEnum;
import com.ph.shopping.common.util.page.PageBean;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.agent.vo.AgentVo;
import com.ph.shopping.facade.merchant.entity.Merchant;
import com.ph.shopping.facade.merchant.service.IMerchantService;
import com.ph.shopping.facade.permission.vo.SessionRoleVo;
import com.ph.shopping.facade.permission.vo.SessionUserVo;
import com.ph.shopping.facade.product.service.IProductService;
import com.ph.shopping.facade.product.vo.PhProductVo;
import com.ph.shopping.facade.supplier.service.ISupplierService;

/**
 * @项目：phshopping-facade-
 *
 * 						@描述： 全国本地商品控制层
 *
 * 						@作者： yyg
 *
 * @创建时间：2017年3月14日 下午1:14:28
 *
 * @Copyright by yyg
 */

@Controller
@RequestMapping("/api/countrywideAndLocalProduct")
public class CountrywideAndLocalProductController extends BaseController {

	@Reference(version = "1.0.0")
	private IProductService iProductService;

	// 供应商
	@Reference(version = "1.0.0")
	private ISupplierService iSupplierService;

	// 代理商
	@Reference(version = "1.0.0")
	public IMerchantService iMerchantService;

	/**
	 * 本地商品列表查询页面跳转
	 * 
	 * @author yyg
	 * @throws Exception
	 */
	@RequestMapping(value = "/localListPage")
	public String listPage(Model model, HttpServletRequest request) throws Exception {
		SessionUserVo info = getLoginUser(request);
		SessionRoleVo role = info.getSessionRoleVo().get(0);
		if (role.getRoleCode() == RoleEnum.MERCHANT.getCode()) {// 为商户的时候要查询他上级代理商的本地商品
			Merchant merchant = new Merchant();
			merchant.setId(info.getId());
			Result result = iMerchantService.getCityAgentByMerchant(merchant);
			Long agentId = null;
			logger.info("通过商户id查询市级代理商id返回值，result={}", JSON.toJSONString(result));
			if (null != result && ResultEnum.SUCCESS.getCode().equals(result.getCode()) && null != result.getData()) {
				agentId = ((AgentVo) result.getData()).getId();
				model.addAttribute("createUser", agentId);
			}
			if (null == agentId) {
				model.addAttribute("createUser", "-1");
			}
		} else if (role.getRoleCode() == RoleEnum.ADMIN.getCode()) {
			model.addAttribute("createUser", "");
		} else {
			model.addAttribute("createUser", info.getId());
		}
		return "product/products/countrywideAndLocalProduct/localList";
	}

	/**
	 * 本地商品列表列表查询
	 * 
	 * @author yyg
	 * @param phProductVo
	 * @return Result
	 */
	@RequestMapping(value = "/getLocalPageList", method = RequestMethod.GET)
	public @ResponseBody Object getPageList(PageBean pageBean, PhProductVo phProductVo) throws Exception {

		// 查询未删除商品
		phProductVo.setDeleteFlag(1);
		// 查询已经审核的商品
		phProductVo.setAuditState(3);
		// 查询本地商品
		phProductVo.setProductType(2);

		return iProductService.getProductVoPageList(pageBean, phProductVo);
	}

	/**
	 * 全国商品列表查询页面跳转
	 * 
	 * @author yyg
	 * @throws Exception
	 */
	@RequestMapping(value = "/countryListPage")
	public String countryListPage(Model model, HttpServletRequest request) throws Exception {
		SessionUserVo info = getLoginUser(request);
		SessionRoleVo role = info.getSessionRoleVo().get(0);
		Long agentId = null;
		if (role.getRoleCode() ==RoleEnum.MERCHANT.getCode()) {// 为商户的时候要查询他上级代理商的本地商品
			Merchant merchant = new Merchant();
			merchant.setId(info.getId());
			Result result = iMerchantService.getCityAgentByMerchant(merchant);
			logger.info("通过商户id查询市级代理商id返回值，result={}", JSON.toJSONString(result));
			if (null != result && ResultEnum.SUCCESS.getCode().equals(result.getCode()) && null != result.getData()) {
				agentId = ((AgentVo) result.getData()).getId();
				model.addAttribute("createUser", "");
			}
			if (null == agentId) {
				model.addAttribute("createUser", "-1");
			}
		} else {
			model.addAttribute("createUser", "");
		}

		return "product/products/countrywideAndLocalProduct/countryList";
	}

	/**
	 * 全国商品列表列表查询
	 * 
	 * @author yyg
	 * @param phProductVo
	 * @return Result
	 */
	@RequestMapping(value = "/getCountryPageList", method = RequestMethod.GET)
	public @ResponseBody Object getCountryPageList(PageBean pageBean, PhProductVo phProductVo) throws Exception {
		// 查询未删除商品 @RequestParam("pType") Integer pType
		// System.err.println(phProductVo.getCreateUser());
		phProductVo.setDeleteFlag(1);
		// 查询已经审核的商品
		phProductVo.setAuditState(3);
		// 查询全国商品
		phProductVo.setProductType(1);
		return iProductService.getProductVoPageList(pageBean, phProductVo);
	}

}
